import { Profile } from './user/profile.js';
import { OrgSettings } from './merchantOrg/orgSettings.js';
import { MerchantRetailSettings } from './retail/merchantRetailSettings.js';
import { Definition } from './common/definition.js';
import { Storage } from '@/utils/storage.js';
import networkCache from '../network/networkCache'

export const Engine = {
    profile: null,
    versionInfo: null,
    printer: null,
    launchArgs: [],
    deviceId: '',
    deviceType: 'desktop',
    deviceModel: '',
    hideHomePageIncomeData: false,
    enableRetailPosPrint: false,
    storage: null,
    // notificationMgr: new PlatformNotificationMgr(),
    async init(isWeb = false) {
        Definition.isWeb = isWeb;
       
        Engine.storage = new Storage();
        
        try {
            this.deviceId = Engine.storage.getStorageSync('deviceId') || '';
            const userDataJsonStr = Engine.storage.getStorageSync('userData');
            if (userDataJsonStr) {
                this.profile = Profile.fromStoreJson(JSON.parse(userDataJsonStr));
            }
        } catch (e) {
            LogUtil.v('Parse user data failed: ' + e);
        }
    },

    clear(){
        // todo 把Engine  定义为 class, 重新构建一个对象，不用再 clear
         Engine.profile = null;
         Engine.versionInfo = null;
         Engine.printer = null;
         Engine.launchArgs = [];
         Engine.deviceId = '';
         Engine.deviceType = 'desktop';
         Engine.deviceModel = '';
         Engine.hideHomePageIncomeData = false;
         Engine.enableRetailPosPrint = false;
         Engine.storage = null;

         // 清除所有缓存
         networkCache.clearAll();
    },

    saveDeviceIdData() {
        Engine.storage.setStorageSync('deviceId', this.deviceId);
    },

    saveUserInfo() {
        if (this.profile && this.profile.userName !== 'linghang') {
            Engine.storage.setStorageSync('userData', JSON.stringify(this.profile.toStoreJson()));
        }
    },

    setProfile(profile) {
        this.profile = profile;
    },

    getCurrencyCharector() {
        return this.profile?.org?.currencyCharector || '¥';
    },

    getPrintCurrencyCharector() {
        return this.profile?.org?.printCurrencyCharector || '￥';
    },

    getOrgSettings() {
        return this.profile?.org?.settings || new OrgSettings();
    },

    getRetailSettings() {
        return this.profile?.org?.retailSettings || new MerchantRetailSettings();
    },

    setOrgSetting(orgSettings) {
        this.profile?.org?.updateSettings(orgSettings);
    },

    setRetailSetting(settings) {
        this.profile?.org?.updateRetailSettings(settings);
    },

    getPrecision() {
        if (Definition.isAutoTestMode) return 4;
        return this.profile?.org?.settings?.precision ?? Definition.defaultPrecisionLen;
    },

    getFundPrecision() {
        if (Definition.isAutoTestMode) return 4;
        return this.profile?.org?.settings?.fundPrecision ?? Definition.gDefaultFundPrecision;
    },

    showStoreNameForOrders() {
        const settings = this.getOrgSettings();
        return settings.multiStoreMode || settings.forceToShowOrderStoreInfo;
    },

};
/*

import WechatUtil from './wechat/wechat_util.js';
import BasicUIAction from '../../engine/uiActions/basicUIAction.js';
import CommunicationUtil from '../network/communication_converter.js';
import NetworkUtil from '../network/network_util.js';
import LogUtil from '../utils/log_util.js';
import PlatformNotificationMgr from './platform/platform_notification_mgr.js';
import LHPrinter from './print/lh_printer.js';
import VersionInfo from './backendConfig/appUpdator.js'; // 假设此为版本处理模块
import LocaleModel from './localization/local_module.js';

const Engine = {
    profile: null,
    versionInfo: null,
    printer: null,
    launchArgs: [],
    deviceId: '',
    deviceType: 'phone',
    deviceModel: '',
    hideHomePageIncomeData: false,
    enableRetailPosPrint: false,
    notificationMgr: new PlatformNotificationMgr(),

    async init() {
        CommunicationUtil.initFunFactory(NetworkUtil.fromJsonfactories);

        try {
            this.deviceId = Engine.storage.getStorageSync('deviceId') || '';

            const userDataJsonStr = Engine.storage.getStorageSync('userData');
            if (userDataJsonStr) {
                this.profile = Profile.fromStoreJson(JSON.parse(userDataJsonStr));
            }
        } catch (e) {
            LogUtil.v('Parse user data failed: ' + e);
        }

        this.notificationMgr.setReadedInfoList(this.getNotificationReadState());

        if (Definition.isMobileMode) {
            WechatUtil.initWechatSDK();
        }

        if (!Definition.isUnitTestMode) {
            const systemInfo = uni.getSystemInfoSync();
            const versionStr = systemInfo.appVersion || '1.0.0';
            this.versionInfo = VersionInfo.fromVersionString(versionStr);
        }

        const printerDataJsonStr = Engine.storage.getStorageSync('printerChoiceData');
        if (printerDataJsonStr) {
            try {
                this.printer = LHPrinter.fromStoreJson(JSON.parse(printerDataJsonStr));
            } catch (e) {
                LogUtil.v('Parse printer choice data failed: ' + e);
            }
        }

        if (!Definition.isUnitTestMode) {
            this.printer = this.printer || new LHPrinter();
            this.printer.init?.();
        }

        LogUtil.initV2?.();
        CommunicationUtil.reloginCallback = BasicUIAction.reLogin;

        this.initHomePageIncomeDataShowConfig();
        this.initRetailPosPrinter();
        await this.initDeviceInfo();
    },

    async initDeviceInfo() {
        try {
            const info = uni.getSystemInfoSync();
            this.deviceModel = info.model || '';
        } catch (e) {
            this.deviceModel = 'unknown';
        }
        console.log('deviceModel:', this.deviceModel);
    },

   

    showStoreNameForOrders() {
        const settings = this.getOrgSettings();
        return settings.multiStoreMode || settings.forceToShowOrderStoreInfo;
    },

    initHomePageIncomeDataShowConfig() {
        const val = Engine.storage.getStorageSync('hideHomePageIncomeData');
        this.hideHomePageIncomeData = val === 'true';
    },

    setHomePageIncomeDataHideConfig(hide) {
        this.hideHomePageIncomeData = hide;
        Engine.storage.setStorageSync('hideHomePageIncomeData', hide ? 'true' : 'false');
    },

    initRetailPosPrinter() {
        const val = Engine.storage.getStorageSync('enableRetailPosPrint');
        this.enableRetailPosPrint = val === 'true';
    },

    setRetailPosPrinter(enable) {
        this.enableRetailPosPrint = enable;
        Engine.storage.setStorageSync('enableRetailPosPrint', enable ? 'true' : 'false');
    },

    saveUserInfo() {
        if (this.profile && this.profile.userName !== 'linghang') {
            Engine.storage.setStorageSync('userData', JSON.stringify(this.profile.toStoreJson()));
        }
    },

    saveNotificationReadState(readedList) {
        Engine.storage.setStorageSync('notificationReadState', JSON.stringify({ readedList }));
    },

    getNotificationReadState() {
        const ret = [];
        try {
            const jsonStr = Engine.storage.getStorageSync('notificationReadState');
            if (jsonStr) {
                const jsonData = JSON.parse(jsonStr);
                const idList = jsonData.readedList || [];
                return idList;
            }
        } catch (e) {
            LogUtil.v('Parse notification data failed: ' + e);
        }
        return ret;
    },

    savePrinterChoiceData() {
        if (this.printer) {
            Engine.storage.setStorageSync('printerChoiceData', JSON.stringify(this.printer.toStoreJson()));
        }
    },



    createLocalModel() {
        const lang = 0; // 可从用户数据中获取实际语言配置
        return LocaleModel.initWithLang(lang);
    },

    setProfile(profile) {
        this.profile = profile;
    }
};

export default Engine;
*/